home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 27.1 KB | 1,055 lines | [TEXT/MPS ] |
- // USection.cp
- // Copyright © 1984-1991 by Apple Computer Inc. All rights reserved.
-
- #ifndef __USECTION__
- #include <USection.h>
- #endif
-
- #ifndef __ULIST__
- #include <UList.h>
- #endif
-
- #ifndef __ALIASES__
- #include <Aliases.h>
- #endif
-
- #ifndef __UFILE__
- #include <UFile.h>
- #endif
-
- #ifndef __USTREAM__
- #include <UStream.h>
- #endif
-
- #ifndef __UDESIGNATOR__
- #include <UDesignator.h>
- #endif
-
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #ifndef __UEVENT__
- #include <UEvent.h>
- #endif
-
- #ifndef __UCOMMAND__
- #include <UCommand.h>
- #endif
-
- #ifndef __UEVTHANDLER__
- #include <UEvtHandler.h>
- #endif
-
- #ifndef __UDOCUMENT__
- #include <UDocument.h>
- #endif
-
- #ifndef __UFILEBASEDDOCUMENT__
- #include <UFileBasedDocument.h>
- #endif
-
- #ifndef __BALLOONS__
- #include <Balloons.h>
- #endif
-
- #ifndef __UVIEW__
- #include <UView.h>
- #endif
-
- #ifndef __UADORNERS__
- #include <UAdorners.h>
- #endif
-
- #ifndef __UEDITIONDOCUMENT__
- #include <UEditionDocument.h>
- #endif
-
- #ifndef __UFILEHANDLER__
- #include <UFileHandler.h>
- #endif
-
- #ifndef __UFAILURE__
- #include <UFailure.h>
- #endif
-
- #ifndef __TEXTEDIT__
- #include <TextEdit.h>
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include <UMacAppUtilities.h>
- #endif
-
- #ifndef __UPATCH__
- #include <UPatch.h>
- #endif
-
- #ifndef __UMEMORY__
- #include <UMemory.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include <UMacAppGlobals.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- const short kDirty = TRUE; // section IS dirty
- const short kNotDirty = FALSE; // section ISN'T dirty
-
- //--------------------------------------------------------------------------------------------------
- //******************************************************************************************
- // T S e c t i o n
- //******************************************************************************************
- #pragma segment MASectionRes
-
- pascal void TSection::ISection(TEditionDocument* itsDocument,
- TDesignator* itsDesignator,
- SectionHandle itsSectionHandle,
- short itsRsrcID)
-
- {
- this->IObject();
-
- fDocument = itsDocument;
- fDesignator = itsDesignator;
- fSectionHandle = itsSectionHandle;
- fRsrcID = itsRsrcID;
- fFound = kFound;
- fCanceled = kDontCancel;
- this->MarkDirty();
- fNewSection = TRUE; // yes it is new
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TSection::Associate(FSSpecPtr newSectionDocument)
-
- {
- if (qDebug && !fSectionHandle)
- ProgramBreak("###fSectionHandle is NULL");
-
- FailOSErr(AssociateSection(fSectionHandle, *newSectionDocument));
- this->MarkDirty();
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSection::AttachSectionHandle(SectionHandle itsSectionHandle)
-
- {
- if (qDebug && fSectionHandle)
- ProgramBreak("###about to overwrite fSectionHandle");
-
- fSectionHandle = itsSectionHandle;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TSection::DoNeedDiskSpace(long& ,
- long& rsrcForkBytes)
-
- {
- if (fSectionHandle)
- rsrcForkBytes += GetHandleSize((Handle)fSectionHandle) + GetHandleSize((Handle)(*fSectionHandle)->alias) + fDesignator->GetSpecificationSize() + sizeof(IDType);
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionClose
-
- pascal void TSection::Free(void)
-
- {
- fDocument->RemoveSection(this);
-
- this->UnRegister();
- fDesignator = (TDesignator *)FreeIfObject(fDesignator);
- fSectionHandle = DisposeIfSectionHandle(fSectionHandle);
- inherited::Free();
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSection::GetEditionName(Str255& name)
-
- {
- this->UpdateEditionInfo();
- name = fLastEditionInfo.container.theFile.name;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal TFileHandler* TSection::GetFileHandler(void)
-
- {
- if (fDocument)
- return fDocument->GetFileHandler();
- else
- return NULL;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal SectionType TSection::GetSectionType(void)
-
- {
- if (fSectionHandle)
- return (*fSectionHandle)->kind;
- else
- return (SectionType) - 1;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal UpdateMode TSection::GetUpdateMode(void)
-
- {
- if (fSectionHandle)
- return (*fSectionHandle)->mode;
- else
- return (UpdateMode) - 1;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRead
-
- pascal void TSection::DoRead(TFile* aFile)
-
- {
- if (!aFile->IsRsrcForkOpen())
- {
- if (qDebug)
- ProgramBreak("###resource fork isn''t open");
- }
- else
- {
- FailInfo fi;
-
- // set up for failure handler
- TResourceStream * aResourceStream = NULL;
- short oldResFile;
-
- VOLATILE(aResourceStream);
- VOLATILE(oldResFile);
-
- fSectionHandle = NULL;
- fDesignator = NULL;
-
- if (fi.Try())
- {
- Handle aHandle;
- IDType designatorID;
-
- oldResFile = aFile->UseResource(); // set the current ResFile
-
- // read in the section handle from a rSectionType resource
- aHandle = Get1Resource(rSectionType, fRsrcID);
- FailResError();
- DetachResource(aHandle);
- FailResError();
- fSectionHandle = (SectionHandle)aHandle;
- (*fSectionHandle)->alias = NULL; // so failure handler works
-
- // read in the alias handle from a rAliasType resource
- aHandle = Get1Resource(rAliasType, fRsrcID);
- FailResError();
- DetachResource(aHandle);
- FailResError();
- (*fSectionHandle)->alias = (AliasHandle)aHandle;
-
- // read in the associated designator
- aResourceStream = new TResourceStream;
- aResourceStream->IResourceStream(kDesignatorResType, fRsrcID);
-
- aResourceStream->ReadLong(*((long*)&designatorID)); // read in the signature
- fDesignator = (TDesignator *)NewStdObject(designatorID);
- fDesignator->InitFromStream(fDocument, aResourceStream);
-
- aResourceStream->Free();
-
- UseResFile(oldResFile); // restore the current ResFile
-
- fi.Success();
- }
- else // Recover
- {
- // free any memory that was allocated
- aResourceStream = (TResourceStream *)FreeIfObject(aResourceStream);
- fSectionHandle = DisposeIfSectionHandle(fSectionHandle);
- fDesignator = (TDesignator*) FreeIfObject(fDesignator);
-
- UseResFile(oldResFile); // restore the current ResFile
-
- fi.ReSignal(); // Optional. Omit to continue.
- }
-
- // register the section
- fFound = kFound; // let's assume that it's found
- fNewSection = FALSE; // no it is not new
- OSErr err = this->Register();
- if (err == containerNotFoundWrn)
- fFound = kNotFound;
- else if (err != noErr)
- FailOSErr(err);
- }
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TSection::DoWrite(TFile* aFile,
- Boolean)
-
- {
- FSSpec itsFile;
- Str255 rsrcName;
- Handle aHandle;
- EditionInfoRecord editionInfo;
- IDType designatorID;
-
- if (!fSectionHandle)
- {
- if (qDebug)
- ProgramBreak("###SectionHandle is NULL");
- }
- else
- {
- if (!aFile->IsRsrcForkOpen())
- {
- if (qDebug)
- ProgramBreak("###resource fork isn't open");
- }
- else
- {
- FailInfo fi;
- TResourceStream * aResourceStream = NULL;
- short oldResFile;
-
- VOLATILE(aResourceStream);
- VOLATILE(oldResFile);
-
- if (fi.Try())
- {
- oldResFile = aFile->UseResource(); // set the current ResFile
-
- // ensure that the section object knows that it belongs to the file
- aFile->GetFileSpec(itsFile);
- this->Associate(&itsFile);
-
- // prepare to write out our section resources
- FailOSErr(GetEditionInfo(fSectionHandle, editionInfo));
- rsrcName = editionInfo.container.theFile.name;
-
- // add the alias handle as an rAliasType resource
- aHandle = (Handle)(*fSectionHandle)->alias;
- FailOSErr(HandToHand(aHandle));
- AddResource(aHandle, rAliasType, fRsrcID, rsrcName);
- FailResError();
-
- // add the section handle as an rSectionType resource
- aHandle = (Handle)fSectionHandle;
- FailOSErr(HandToHand(aHandle));
- AddResource(aHandle, rSectionType, fRsrcID, rsrcName);
- FailResError();
-
- // ask the designator to write its specification to a TResourceStream
- aHandle = NewPermHandle(2);
- AddResource(aHandle, kDesignatorResType, fRsrcID, rsrcName);
- FailResError();
- WriteResource(aHandle);
- ReleaseResource(aHandle);
-
- // write the designator to a resource stream
- aResourceStream = new TResourceStream;
- aResourceStream->IResourceStream(kDesignatorResType, fRsrcID);
-
- // ??? replace the next 3 lines with aResourceStream->WriteStreamObject(fDesignator)
- designatorID = fDesignator->GetSignature();
- aResourceStream->WriteLong(designatorID); // write its signature
- fDesignator->WriteToStream(aResourceStream);// write its specification
-
- aResourceStream->Free();
-
- UseResFile(oldResFile); // restore the current ResFile
-
- fDirty = kNotDirty; // it's been saved, it's not dirty
- fNewSection = FALSE; // no it is not new
- fi.Success();
- }
- else // Recover
- {
- aResourceStream = (TResourceStream *)FreeIfObject(aResourceStream);
-
- UseResFile(oldResFile); // restore the current ResFile
- fi.ReSignal(); // Optional. Omit to continue.
- }
-
- }
- }
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TSection::Delete(void)
-
- {
- // NOTE: may only need to do this if the file handler's fHowToSave is save-in-place!!!
- Handle h;
-
- // remove the alias handle from the resource fork
- h = Get1Resource(rAliasType, fRsrcID);
- if (h)
- {
- RmveResource(h);
- FailResError();
- h = DisposeIfHandle(h);
- }
-
- // remove the section handle from the resource fork
- h = Get1Resource(rSectionType, fRsrcID);
- if (h)
- {
- RmveResource(h);
- FailResError();
- h = DisposeIfHandle(h);
- }
-
- // remove the designator handle from the resource fork
- h = Get1Resource(kDesignatorResType, fRsrcID);
- if (h)
- {
- RmveResource(h);
- FailResError();
- h = DisposeIfHandle(h);
- }
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal Boolean TSection::IsRegistered(void)
-
- {
- // it's registered if the Edition Manager thinks it's registered
- if (fSectionHandle)
- return IsRegisteredSection(fSectionHandle) == noErr;
- else
- return FALSE;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal Boolean TSection::IsSectionSelected(void)
-
- {
- if (fDocument)
- return fDocument->IsSectionSelected(this);
- else
- return FALSE;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSection::MarkDirty(void)
-
- {
- fDirty = kDirty;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal Boolean TSection::IsDirty(void)
-
- {
- return fDirty;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal OSErr TSection::Register(void)
-
- {
- OSErr err = noErr;
-
- if (!this->IsRegistered())
- {
- TFileHandler * aFileHandler = this->GetFileHandler();
- if (aFileHandler && aFileHandler->FileExists())
- {
- if (aFileHandler->fFile)
- {
- FSSpec theFileSpec;
-
- Boolean aliasWasUpdated;
- aFileHandler->fFile->GetFileSpec(theFileSpec);
- err = RegisterSection(theFileSpec, fSectionHandle, aliasWasUpdated);
-
- // if (aliasWasUpdated, mark section as dirty so that user saves changed edition
- if (aliasWasUpdated)
- this->MarkDirty();
-
- // if (RegisterSection was successful, update edition info
- if (err == noErr)
- this->UpdateEditionInfo();
- }
- else
- {
- err = fnfErr;
- if (qDebug)
- ProgramBreak("how to call RegisterSection if there isn't a FileSpec???");
- }
- }
- else
- {
- err = fnfErr;
- if (qDebug)
- ProgramBreak("how to call RegisterSection if there isn't a TFile???");
- }
- }
- return err;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSection::UnRegister(void)
-
- {
- if (this->IsRegistered())
- FailOSErr(UnRegisterSection(fSectionHandle));
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSection::UpdateEditionInfo(void)
-
- {
- EditionInfoRecord editionInfo;
-
- if (this->IsRegistered() && (GetEditionInfo(fSectionHandle, editionInfo) == noErr))
- fLastEditionInfo = editionInfo;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSection::CancelSection(Boolean cancel)
-
- {
- OSErr err;
-
- if (cancel != fCanceled)
- if (cancel == kCancel)
- {
- this->UnRegister();
- fCanceled = kCancel; // it IS canceled
- }
- else
- {
- err = this->Register();
- if ((err != noErr) && (err != containerNotFoundWrn) && (err != notThePublisherWrn) && (err != multiplePublisherWrn))
- FailOSErr(err);
- fCanceled = kDontCancel; // it IS !canceled
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TSection::Fields(TObject* obj) // override
-
- {
- long aLongint;
- Boolean isARegisteredSection;
-
- obj->DoToField("TSection", NULL, bClass);
- obj->DoToField("fDocument", &fDocument, bObject);
- obj->DoToField("fDesignator", &fDesignator, bObject);
- aLongint = (long)fSectionHandle;
- obj->DoToField("fSectionHandle", &aLongint, bLongInt);
- if (fSectionHandle)
- {
- SectionRecord aSectionRecord = **fSectionHandle;
- obj->DoToField("**fSectionHandle", &aSectionRecord, bSectionRecord);
- }
- obj->DoToField("fRsrcID", (Ptr) & fRsrcID, bInteger);
- obj->DoToField("fFound", (Ptr) & fFound, bBoolean);
- obj->DoToField("fCanceled", (Ptr) & fCanceled, bBoolean);
- obj->DoToField("fDirty", (Ptr) & fDirty, bBoolean);
-
- isARegisteredSection = this->IsRegistered();
- obj->DoToField("isARegisteredSection", &isARegisteredSection, bBoolean);
-
- inherited::Fields(obj);
- }
-
- //******************************************************************************************
- // T P u b l i s h e r
- //******************************************************************************************
- #pragma segment MASectionRes
-
- pascal void TPublisher::IPublisher(TEditionDocument* itsDocument,
- TDesignator* itsDesignator,
- SectionHandle itsSectionHandle,
- short itsRsrcID)
-
- {
- this->ISection(itsDocument, itsDesignator, itsSectionHandle, itsRsrcID);
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TPublisher::DoUpdate(ChangeID& theChange,
- TObject* changedObject,
- TObject* changedBy)// override
-
- {
- unsigned long secs;
-
- // mark this publisher as changed
- GetDateTime(secs);
- this->MarkAsChanged(secs);
-
- inherited::DoUpdate(theChange, changedObject, changedBy);
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TPublisher::Publish(void)
-
- {
- EditionRefNum refNum;
- FSSpec aFSSpec;
- FSSpecPtr aFSSpecPtr = NULL;
- FailInfo fi;
- OSErr err;
- TSectionStream* aSectionStream;
-
- VOLATILE(refNum);
-
- // open the edition file
- TFileHandler* aFileHandler = this->GetFileHandler();
- if (aFileHandler && aFileHandler->FileExists())
- {
- aFileHandler->fFile->GetFileSpec(aFSSpec);
- aFSSpecPtr = &aFSSpec;
- }
- // ??? It's dangerous to dereference aFSSpecPtr on the next line when it could be NULL,
- // however, the declaration of OpenNewEdition() requires an FSSpec&.
- err = OpenNewEdition(fSectionHandle, aFileHandler->fFile->fCreator, *aFSSpecPtr, refNum);
- if (err == noErr) /* DON'T signal failure if the OpenNewEdition
- fails, just continue without publishing */
- {
- if (fi.Try())
- {
- // Create a section stream to write the published data to.
- aSectionStream = new TSectionStream;
- aSectionStream->ISectionStream(refNum, ' ');
-
- /* The order in which the application publishes to the edition container file indicates
- to the client application the preferred order for subscribers! So, publish the TEXT
- or PICT or 'snd ' (at least one is required) in the order prefered.
- N.B.: The Edition Manager will set the edition container file's file type
- according to which type was published first: 'edtp' if PICT was published first,
- 'edtt' if TEXT was published first, and 'edts' if 'snd ' was published first. */
- if (fDocument->GetPublishPreference() == 'PICT')
- {
- aSectionStream->Reset('PICT', 0);
- fDocument->DoWriteData('PICT', fDesignator, aSectionStream);
- aSectionStream->Reset('TEXT', 0);
- fDocument->DoWriteData('TEXT', fDesignator, aSectionStream);
- aSectionStream->Reset('snd ', 0);
- fDocument->DoWriteData('snd ', fDesignator, aSectionStream);
- }
- else if (fDocument->GetPublishPreference() == 'TEXT')
- {
- aSectionStream->Reset('TEXT', 0);
- fDocument->DoWriteData('TEXT', fDesignator, aSectionStream);
- aSectionStream->Reset('PICT', 0);
- fDocument->DoWriteData('PICT', fDesignator, aSectionStream);
- aSectionStream->Reset('snd ', 0);
- fDocument->DoWriteData('snd ', fDesignator, aSectionStream);
- }
- else if (fDocument->GetPublishPreference() == 'snd ')
- {
- aSectionStream->Reset('snd ', 0);
- fDocument->DoWriteData('snd ', fDesignator, aSectionStream);
- aSectionStream->Reset('TEXT', 0);
- fDocument->DoWriteData('TEXT', fDesignator, aSectionStream);
- aSectionStream->Reset('PICT', 0);
- fDocument->DoWriteData('PICT', fDesignator, aSectionStream);
- }
-
- // last priority: publish the application's custom data (this is optional)
- aSectionStream->Reset(fDocument->fScrapType, 0);
- fDocument->DoWriteData(fDocument->fScrapType, fDesignator, aSectionStream);
-
- // Free the stream
- aSectionStream->Free();
-
- fi.Success();
- }
- else // Recover
- {
- // Free the stream
- aSectionStream->Free();
-
- // If there is a failure during publishing close the edition file without saving
- // the changes (pass FALSE for successful). This ensures that the publisher will
- // get a shot at publishing again later.
- err = CloseEdition(refNum, FALSE);
-
- fi.ReSignal(); // Optional. Omit to continue.
- }
- // close the edition file, passing TRUE for the successful parameter
- FailOSErr(CloseEdition(refNum, TRUE));
- }
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TPublisher::DoWrite(TFile* aFile,
- Boolean makingCopy)// override
-
- {
- inherited::DoWrite(aFile, makingCopy);
-
- if ((!fDocument->fStopAllEditions) && fSectionHandle && (this->GetUpdateMode() == pumOnSave) && this->IsChanged())
- this->Publish();
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TPublisher::Delete(void) // override
-
- {
- this->DeleteEditionFile();
-
- inherited::Delete();
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TPublisher::DeleteEditionFile(void)
-
- {
- FSSpec theFile;
-
- // get the edition container file's FSSpec - from updated fLastEditionInfo
- if (this->IsRegistered())
- this->UpdateEditionInfo();
- theFile = fLastEditionInfo.container.theFile;
-
- // cancel the section - this unregisters the section
- this->CancelSection(kCancel);
-
- // delete the edition container file
- FailOSErr(DeleteEditionContainerFile(theFile));
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal Boolean TPublisher::IsChanged(void)
-
- {
- EditionInfoRecord editionInfo;
-
-
- if (fSectionHandle)
- {
- FailOSErr(GetEditionInfo(fSectionHandle, editionInfo));
- return editionInfo.mdDate != (*fSectionHandle)->mdDate;
- }
- else
- return FALSE;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TPublisher::MarkAsChanged(TimeStamp mdDate)
-
- {
- if (fSectionHandle)
- (*fSectionHandle)->mdDate = mdDate;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TPublisher::Fields(TObject* obj) // override
-
- {
- obj->DoToField("TPublisher", (Ptr)NULL, bClass);
-
- inherited::Fields(obj);
- }
-
- //******************************************************************************************
- // T S u b s c r i b e r
- //******************************************************************************************
- #pragma segment MASectionRes
-
- pascal void TSubscriber::ISubscriber(TEditionDocument* itsDocument,
- TDesignator* itsDesignator,
- SectionHandle itsSectionHandle,
- short itsRsrcID)
-
- {
- this->ISection(itsDocument, itsDesignator, itsSectionHandle, itsRsrcID);
-
- // ensure that the document is notified of any and all changes to the subscriber
- this->AddDependent(itsDocument);
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRead
-
- pascal void TSubscriber::SubscribeIfNewer(void)
-
- {
- if ((!fDocument->fStopAllEditions) && fSectionHandle && (this->GetUpdateMode() == sumAutomatic) && this->IsNewerEditionAvailable())
- this->Subscribe();
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRead
-
- pascal void TSubscriber::Subscribe(void)
-
- {
- EditionRefNum refNum;
- FailInfo fi;
- Size formatSize;
- OSErr err;
- TSectionStream* aSectionStream;
-
- VOLATILE(refNum);
-
- FailOSErr(OpenEdition(fSectionHandle, refNum));
-
- if (fi.Try())
- {
- // Create a section stream to read the published data from.
- aSectionStream = new TSectionStream;
- aSectionStream->ISectionStream(refNum, ' ');
-
- // Top priority: subscribe to private types.
- err = EditionHasFormat(refNum, fDocument->fScrapType, formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset(fDocument->fScrapType, formatSize);
- fDocument->DoReadData(fDocument->fScrapType, fDesignator, aSectionStream, formatSize);
- }
- else
- {
- // subscribe to one or more of the required data types
- err = EditionHasFormat(refNum, 'PICT', formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset('PICT', formatSize);
- fDocument->DoReadData('PICT', fDesignator, aSectionStream, formatSize);
- }
- err = EditionHasFormat(refNum, 'TEXT', formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset('TEXT', formatSize);
- fDocument->DoReadData('TEXT', fDesignator, aSectionStream, formatSize);
- }
- err = EditionHasFormat(refNum, 'snd ', formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset('snd ', formatSize);
- fDocument->DoReadData('snd ', fDesignator, aSectionStream, formatSize);
- }
- }
-
- // Free the stream
- aSectionStream->Free();
-
- fi.Success();
- }
- else // Recover
- {
- // Free the stream
- aSectionStream->Free();
-
- // If there is a failure during subscribing, close the edition file without saving
- // the changes (pass FALSE for successful). This ensures that the subscriber will
- // get a shot at subscribing again later.
- err = CloseEdition(refNum, FALSE);
-
- fi.ReSignal(); // Optional. Omit to continue.
- }
-
- // close the edition file
- FailOSErr(CloseEdition(refNum, TRUE));
-
- this->Changed(cSubscriberChanged, this); // notify dependents
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal Boolean TSubscriber::IsNewerEditionAvailable(void)
-
- {
- EditionInfoRecord editionInfo;
-
- if (fSectionHandle)
- {
- FailOSErr(GetEditionInfo(fSectionHandle, editionInfo));
- return (editionInfo.mdDate != (*fSectionHandle)->mdDate);
- }
- else
- return FALSE;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- pascal void TSubscriber::OpenPublisher(void)
-
- {
- EditionInfoRecord editionInfo;
-
- FailOSErr(GetEditionInfo(fSectionHandle, editionInfo));
- FailOSErr(GoToPublisherSection(editionInfo.container));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TSubscriber::Fields(TObject* obj) // override
-
- {
- obj->DoToField("TSubscriber", (Ptr)NULL, bClass);
-
- inherited::Fields(obj);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamCreate
-
- pascal void TSectionStream::Initialize(void) // override
- {
- inherited::Initialize();
-
- fEditionRefNum = 0;
- fEditionOSType = ' ';
- fPosition = 0;
- fSize = 0;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MAStreamCreate
-
- pascal void TSectionStream::ISectionStream(EditionRefNum itsEditionRefNum, OSType itsEditionOSType)
- {
- this->IStream();
-
- fEditionRefNum = itsEditionRefNum;
- fEditionOSType = itsEditionOSType;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TSectionStream::Reset(OSType itsEditionOSType, long itsSize)
- {
- this->SetPosition(0);
- fEditionOSType = itsEditionOSType;
- fSize = itsSize;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- pascal long TSectionStream::GetPosition(void) // override
- {
- return fPosition;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- pascal void TSectionStream::SetPosition(long newPosition)// override
- {
- fPosition = newPosition;
-
- if (fPosition > fSize) // Adjust the size if necessary.
- fSize = fPosition;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- pascal long TSectionStream::GetSize(void)
- {
- return fSize;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- pascal void TSectionStream::SetSize(long newSize)// override
- {
- fSize = newSize;
-
- if (newSize < fPosition) // If we shrunk the size, then we have to adjust the position.
- fPosition = newSize;
- }
-
- //--------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- pascal void TSectionStream::WriteBytes(const void* p, long count)
- {
- FailOSErr(SetEditionFormatMark(fEditionRefNum, fEditionOSType, this->GetPosition()));
- FailOSErr(WriteEdition(fEditionRefNum, fEditionOSType, p, count));
-
- this->SetPosition(fPosition + count); // Advance the position.
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAStreamRead
-
- pascal void TSectionStream::ReadBytes(void* p, long count) // override
- {
- FailOSErr(SetEditionFormatMark(fEditionRefNum, fEditionOSType, this->GetPosition()));
- FailOSErr(ReadEdition(fEditionRefNum, fEditionOSType, p, count));
-
- this->SetPosition(fPosition + count); // Advance the position.
- }
-
-